#include "stdafx.h"

Input::Input():inputDevices(0)
{
}

Input::~Input()
{
}

void Input::update(float dt)
{
	for (std::vector<InputDevice*>::iterator iter = inputDevices.begin(); iter != inputDevices.end(); ++iter)
	{
		(*iter)->update(dt);
	}
}

void Input::registerInputDevice(InputDevice* device)
{
	assert(device);
	
	// make sure the device is not registered
	for (std::vector<InputDevice*>::iterator iter = inputDevices.begin(); iter != inputDevices.end(); ++iter)
	{
		if ((*iter) == device) return;
	}
	inputDevices.push_back(device);
}

void Input::registerAction(int actionId, const std::string& skey)
{
	std::vector<InputDevice*>::iterator iter = inputDevices.begin();
	for (; iter != inputDevices.end(); ++iter)
	{
		(*iter)->registerAction(actionId, skey);
	}
}

bool Input::isActionDown(int actionId) const
{
	// return true if one of the input device returns true
	std::vector<InputDevice*>::const_iterator iter = inputDevices.begin();
	for (; iter != inputDevices.end(); ++iter)
	{
		if ((*iter)->isActionDown(actionId))
		{
			return true;
		}
	}

	return false;
}

bool Input::didActionJustGoDown(int actionId) const
{
	// return true if one of the input device returns true
	std::vector<InputDevice*>::const_iterator iter = inputDevices.begin();
	for (; iter != inputDevices.end(); ++iter)
	{
		if ((*iter)->didActionJustGoDown(actionId))
		{
			return true;
		}
	}

	return false;
}

bool Input::didActionJustGoUp(int actionId) const
{
	// return true if one of the input device returns true
	std::vector<InputDevice*>::const_iterator iter = inputDevices.begin();
	for (; iter != inputDevices.end(); ++iter)
	{
		if ((*iter)->didActionJustGoUp(actionId))
		{
			return true;
		}
	}

	return false;
}

bool Input::isKeyDown(int key) const
{
	// return true if one of the input device returns true
	std::vector<InputDevice*>::const_iterator iter = inputDevices.begin();
	for (; iter != inputDevices.end(); ++iter)
	{
		if ((*iter)->isKeyDown(key))
		{
			return true;
		}
	}

	return false;
}

bool Input::didKeyJustGoDown(int key) const
{
	// return true if one of the input device returns true
	std::vector<InputDevice*>::const_iterator iter = inputDevices.begin();
	for (; iter != inputDevices.end(); ++iter)
	{
		if ((*iter)->didKeyJustGoDown(key))
		{
			return true;
		}
	}

	return false;
}

bool Input::didKeyJustGoUp(int key) const
{
	// return true if one of the input device returns true
	std::vector<InputDevice*>::const_iterator iter = inputDevices.begin();
	for (; iter != inputDevices.end(); ++iter)
	{
		if ((*iter)->didKeyJustGoUp(key))
		{
			return true;
		}
	}

	return false;
}

void Input::getMove(MoveParam& param) const
{
	for (std::vector<InputDevice*>::const_iterator iter = inputDevices.begin(); iter != inputDevices.end(); ++iter)
	{
		MoveParam tempParam;
		(*iter)->getMove(tempParam);
		param += tempParam;
	}
}